package com.icoopoo.modules.transaction.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.icoopoo.modules.transaction.model.TransactionModel;
import com.icoopoo.modules.transaction.service.TransactionService;
import com.icoopoo.utils.resultResponse.HelperResponse;


/**
 * 
 * @部门:
 * @功能:略
 * @描述: 
 * @作成者:icoopoo
 * @Data:2018年11月29日 下午9:30:11
 * 
 * 必须抛出 运行时异常 才能 被 事务管理回滚事务。
*
 */
@Controller
@RequestMapping("transaction")
public class TransactionController{

	
	Log loger = LogFactory.getLog(TransactionController.class);
	
	@Autowired
	private TransactionService service;
	
	@ResponseBody
	@RequestMapping(value = "/save",method = RequestMethod.GET)
	public Map<String, Object> saveTransaction(HttpServletRequest request, HttpServletResponse response,String id) {
		try{
			TransactionModel model = new TransactionModel();
			model.setId(id);
			model.setMoney(23.2);
			model.setSaveSuccess(true);
			model.setSorce(id);
			service.save(model);
			
			System.out.println("保存成功");
		}catch(Exception e){
			return HelperResponse.errorMap(null);
		}
		return HelperResponse.successMap(null);
	}
	
	@ResponseBody
	@RequestMapping(value = "/update",method = RequestMethod.GET)
	public Map<String, Object> updateTransaction(HttpServletRequest request, HttpServletResponse response,String id) {
		try{
			TransactionModel model = new TransactionModel();
			model.setId(id);
			model.setMoney(43.2);
			model.setSaveSuccess(true);
			model.setSorce(id);
			service.update(model);
			
			System.out.println("更新成功");
		}catch(Exception e){
			return HelperResponse.errorMap(null);
		}
		return HelperResponse.successMap(null);
	}

	
	@ResponseBody
	@RequestMapping(value = "/saveException",method = RequestMethod.GET)
	public Map<String, Object> saveException(HttpServletRequest request, HttpServletResponse response,String id) {
		try{
			TransactionModel model = new TransactionModel();
			model.setId(id);
			model.setMoney(33.2);
			model.setSaveSuccess(true);
			model.setSorce(id);
			service.saveException(model);
			
			System.out.println("保存成功");
		}catch(Exception e){
			return HelperResponse.errorMap(null);
		} catch (Throwable e) {
			e.printStackTrace();
			return HelperResponse.errorMap(null);
		}
		return HelperResponse.successMap(null);
	}
	
	@ResponseBody
	@RequestMapping(value = "/updateException",method = RequestMethod.GET)
	public Map<String, Object> updateException(HttpServletRequest request, HttpServletResponse response,String id) {
		try{
			TransactionModel model = new TransactionModel();
			model.setId(id);
			model.setMoney(3.2);
			model.setSaveSuccess(true);
			model.setSorce(id);
			service.updateException(model);
			
			System.out.println("更新成功");
		}catch(Exception e){
			
			return HelperResponse.errorMap(null);
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return HelperResponse.successMap(null);
	}
}
